<template>
    <view class="content">
        <view class="input-group">
            <view class="input-row border">
                <text class="title">邮箱：</text>
                <m-input type="text" clearable v-model="email" placeholder="请输入邮箱"></m-input>
            </view>
            <view class="input-row border">
                <text class="title">密码：</text>
                <m-input type="password" displayable v-model="password" placeholder="请输入密码"></m-input>
            </view>
			<view class="input-row border">
			    <text class="title">密码验证：</text>
			    <m-input type="password" displayable v-model="passwordchk" placeholder="请输入密码"></m-input>
			</view>
        </view>
        <view class="btn-row">
            <button type="primary" class="primary" @tap="register">注册</button>
        </view>
		<view class="action-row">
		    <navigator url="../reg/phone">手机注册</navigator>
			<text>|</text>
			<navigator url="../login/login">邮箱登录</navigator>
		</view>
    </view>
</template>

<script>
    import service from '../../service.js';
	import network from '../../common/network.js';
	import validator from '../../common/validator.js';
	import sha1 from 'crypto-js/sha1.js'
    import mInput from '../../components/m-input.vue';

    export default {
        components: {
            mInput
        },
        data() {
            return {
                email: '',
				password: '',
				passwordchk: ''
            }
        },
        methods: {
            register() {
                if (!validator.isValidEmail(this.email) || !validator.isValidPassword(this.password)) {
                	return
                }
                
				if (this.password !== this.passwordchk) {
				    uni.showToast({
				        icon: 'none',
				        title: '两次密码不一致'
				    });
				    return;
				}
				
				// 注册
				network.request('/api/frontend/users/', 'POST',
					{
						"email": this.email,
						"password": sha1(this.password).toString()
					},
					function(res) {
						uni.showToast({
						    title: '注册成功'
						});
						uni.navigateTo({
							url: '../login/login'
						});
					});
            }
        }
    }
</script>

<style>
@import '../../common/custom.css';
</style>
